package com.csu.service.serviceImpl;

import com.csu.common.CommonResponse;
import com.csu.entity.Attendance;
import com.csu.mapper.AttendanceMapper;
import com.csu.mapper.ParentStudentMapper;
import com.csu.mapper.UserMapper;
import com.csu.service.AttendanceService;
import com.csu.vo.AttendanceRespVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
@Service
public class AttendanceServiceImpl implements AttendanceService {
    @Autowired
    AttendanceMapper attendanceMapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    ParentStudentMapper parentStudentMapper;

    /**
     *家长获取子女的出勤记录
     */
    @Override
    public CommonResponse<List<AttendanceRespVO>> getAttendance(int parentId) {
        List<AttendanceRespVO> attendanceRespVOList = new ArrayList<>();
        //通过家长id找到子女id（可以有多个）
        List<Integer> childrenIdList = parentStudentMapper.getChildrenIdList(parentId);
        System.out.println(childrenIdList);

        //查询到子女id则继续查
        if(!childrenIdList.isEmpty()){
            //通过子女的id返回结果
            for(Integer userId:childrenIdList){
                //查询到userId的子女的attendanceList，并将其赋给attendanceRespVOList中
                List<Attendance> attendanceList = attendanceMapper.getAttendanceListByUserId(userId);
                for(Attendance attendance: attendanceList){
                    AttendanceRespVO attendanceRespVO = new AttendanceRespVO();
                    attendanceRespVO.setAttendanceDate(attendance.getAttendanceDate());
                    attendanceRespVO.setIsAttendance(attendance.getIsAttendance());
                    attendanceRespVO.setAttendanceId(attendance.getAttendanceId());
                    //通过userId获取userName
                    String username = userMapper.getUsernameById(attendance.getUserId());
                    attendanceRespVO.setUsername(username);
                    attendanceRespVOList.add(attendanceRespVO);
                }
            }
            //查询到出勤记录则成功
            if(!attendanceRespVOList.isEmpty())
                return CommonResponse.createForSuccess("获取出勤记录成功", attendanceRespVOList);
            return CommonResponse.createForSuccess("未查询到子女出勤信息", null);
        }
        return CommonResponse.createForSuccess("未查询到子女出勤信息", null);
    }
}
